package com.whut.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.whut.model.*;
import com.whut.parMapper.ParProblemCollectDetailMapper;
import com.whut.parMapper.ParProblemDescriptionMapper;
import com.whut.parMapper.ParSystemMapper;
import com.whut.parMapper.utils.MapperUtils;
import com.whut.service.impl.CheckInfoServiceImpl;
import com.whut.service.impl.ReportServiceImpl;
import com.whut.utils.FileUtil;
import com.whut.utils.MDoc;
import com.whut.utils.pointsCalculate;
import com.whut.v_model.ProblemCollectDetail;
import org.apache.commons.lang3.StringUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;

import static com.whut.service.impl.CheckInfoServiceImpl.IMG_PATH;

public class QualityReportofCC3EB {

    @Resource
    private UrlProperties urlProperties;
    @Resource
    private Profiles profiles;
    @Resource
    private ParSystemMapper parSystemMapper;
    @Resource
    private ParProblemDescriptionMapper parProblemDescriptionMapper;
    @Resource
    private ParProblemCollectDetailMapper parProblemCollectDetailMapper;
    @Resource
    private CheckInfoServiceImpl checkInfoServiceImpl;
    // @Resource
    @Autowired
    private ReportServiceImpl reportServiceImpl;
    @Resource
    private MapperUtils mapperUtils;

    private int checkId;
    private int projectId;
    private boolean isNew;
    public static final int WIDTH = 718;
    public static final int HEIGHT = 468;
    public QualityReportofCC3EB(ParSystemMapper parSystemMapper,
                                ParProblemDescriptionMapper parProblemDescriptionMapper,
                                ParProblemCollectDetailMapper parProblemCollectDetailMapper,
                                CheckInfoServiceImpl checkInfoServiceImpl,
                                ReportServiceImpl reportServiceImpl,
                                MapperUtils mapperUtils,
                                UrlProperties urlProperties,
                                Profiles profiles
    ) {
        this.parSystemMapper = parSystemMapper;
        this.parProblemDescriptionMapper = parProblemDescriptionMapper;
        this.parProblemCollectDetailMapper = parProblemCollectDetailMapper;
        this.checkInfoServiceImpl = checkInfoServiceImpl;
        this.reportServiceImpl = reportServiceImpl;
        this.mapperUtils=mapperUtils;
        this.urlProperties=urlProperties;
        this.urlProperties=urlProperties;
        this.profiles=profiles;

    }




    public String generateReport(int projectId, int checkId, boolean isNew) throws UnsupportedEncodingException {
        //格式化十进制的类，此处为保留两位小数
        DecimalFormat df = new DecimalFormat("0.00");
        double scAllPoint = 0;
        Integer taskId = mapperUtils.getContractchecktaskMapper().getTaskIdByCheckId(checkId);
        //总的数据结构datamap
        Map<String, Object> dataMap = new HashMap<String, Object>();
        //生成报告的时间
        Date date = new Date();
        DateFormat d1 = new SimpleDateFormat("yyyyMMddhhmmss");
        DateFormat d2 = new SimpleDateFormat("yyyy");
        DateFormat d3 = new SimpleDateFormat("MM");
        DateFormat d4 = new SimpleDateFormat("dd");
        String day = d4.format(date);
        String month = d3.format(date);
        String year = d2.format(date);
        String fileName = d1.format(date);
        dataMap.put("day", day);
        dataMap.put("month", month);
        dataMap.put("year", year);

        //第一部分 --------- 一、工程概况部分，来自ctp的工程信息
        //根据projectId拿到工程信息。其实就一条数据，存在了list里。
        checkInfoServiceImpl.initProjectSummaryFirstPart(projectId, dataMap);

        //第一部分 --------- 二、评估结果 -------（一）综合评估结果
        //质量风险计算部分，有乘权重
        //628number代表体系里的第一级的质量检查，因为是质量报告
        String ZJname1 = "[628]";
        double allsecondPoint = 0;
        JSONArray ZJname1Array = JSONArray.parseArray(ZJname1);
        //根据checkId求总的extraReduce，并转为double
        float allExtraPointFloat = parProblemCollectDetailMapper.getAllExtraPoint(checkId);
        double allExtraPoint = Double.valueOf(String.valueOf(allExtraPointFloat));

        double overAllPoint = 0;

        //第一级所有权重和，这里也就是质量检查的权重
        double allweight1 = 0;
        //只有一个质量检查
        for (int i = 0; i < ZJname1Array.size(); i++) {
            double Tweight1 = 0;
            //根据628number拿到质量检查的权重。
            String weight1 = parSystemMapper.getWeight1ByName1(parSystemMapper.getName4ByName4Id(ZJname1Array.getIntValue(i)));
            //权重String转为double
            Tweight1 = pointsCalculate.getPercentInstance(weight1);
            allweight1 += Tweight1;
        }

        for (int k = 0; k < ZJname1Array.size(); k++) {
            //质量检查
            String pName1 = parSystemMapper.getName4ByName4Id(ZJname1Array.getIntValue(k));
            //和上面一样，拿质量检查的权重double
            double Tweight1 = 0;
            String weight1 = parSystemMapper.getWeight1ByName1(pName1);
            Tweight1 = pointsCalculate.getPercentInstance(weight1);

            //根据checkId和质量检查拿到第二级的number列表，结果同checkId和628=preNum2
            List<Object> secondIdList = parProblemCollectDetailMapper.getSecondId(checkId, pName1);
            //list转json数组
            String secondIdInfo = JSON.toJSONString(secondIdList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray secondIdInfoArray = JSONArray.parseArray(secondIdInfo);

            //求质量检查下的第二级权重和。要不要更改？
            double allweight2 = 0;
            for (int p = 0; p < secondIdInfoArray.size(); p++) {
                int allsecondId = secondIdInfoArray.getIntValue(p);
                String weight2 = parSystemMapper.getWeight3ByThirdId(allsecondId);
                double weight22 = pointsCalculate.getPercentInstance(weight2);
                allweight2 += weight22;
            }

            //2、4、6遍历
            for (int u = 0; u < secondIdInfoArray.size(); u++) {
                int secondId = secondIdInfoArray.getIntValue(u);

                //第二级每一项的权重。二级的权重，这里要不要更改？？？
                double weight222 = 0;
                String weight2 = parSystemMapper.getWeight3ByThirdId(secondId);
                weight222 = pointsCalculate.getPercentInstance(weight2);

                //取第四级的ProblemCollectDetail列表，并转为数组，之后遍历。根据fourthId去重
                //测试“checkId:41;secondId:629
                List<ProblemCollectDetail> fourthPoints = parProblemCollectDetailMapper.getFourthPoint(checkId, pName1, secondId);
                String fourthCalculateInfo = JSON.toJSONString(fourthPoints, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray fourthPointsCalculateArray = JSONArray.parseArray(fourthCalculateInfo);

                double point = 0;
                double point11 = 0;
                for (int j = 0; j < fourthPointsCalculateArray.size(); j++) {
                    Map<String, Object> part2InnerMap = new HashMap<String, Object>();
                    JSONObject part2InnerObject = fourthPointsCalculateArray.getJSONObject(j);
                    part2InnerMap.put("pName4", part2InnerObject.getString("pName4"));

                    //没用的一段代码
                    double weight3 = 0;
                    int part2FourthId = part2InnerObject.getIntValue("fourthId");
                    int part2InnerProblemNum = parProblemCollectDetailMapper.getProblemNumByFourthId(checkId, part2FourthId);
                    int part2ThirdId = part2InnerObject.getIntValue("thirdId");
                    //                   weight3 = pointsCalculate.getPercentInstance(part2InnerObject.getString("weight3"));
                    String newWeight33 = mapperUtils.getEditableweightpsMapper().getWeightByNumber(part2ThirdId, taskId);
                    if (StringUtils.isNotBlank(newWeight33)) {
                        weight3 = pointsCalculate.getPercentInstance(newWeight33);
                    } else {
                        weight3 = pointsCalculate.getPercentInstance(part2InnerObject.getString("weight3"));
                    }

                    //没用的一段代码。拿到第四级的number，并转为数组
                    List<Object> fourthCountList = parProblemCollectDetailMapper.getFourthCountList(checkId, part2ThirdId);
                    String fourthCountInfo = JSON.toJSONString(fourthCountList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                    JSONArray fourthCountInfoArray = JSONArray.parseArray(fourthCountInfo);
                    int countFourthId = fourthCountInfoArray.size();

                    //以下代码都是为了算checkItemPoint，thirdPoint是总的
                    //取第四级的ProblemCollectDetail列表，并转为数组，之后遍历。根据sixthId去重
                    List<ProblemCollectDetail> sixthPoints = parProblemCollectDetailMapper.getsixthPoints(checkId, part2FourthId);
                    String pointName6Info = JSON.toJSONString(sixthPoints, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                    JSONArray sixthPointsArray = JSONArray.parseArray(pointName6Info);

                    //减过分
                    double calculatePoint6 = 0;
                    //暂存每次循环计算的分
                    double calculatePoint66 = 0;
                    //不减分
                    double calculateTruePoint = 0;
                    //遍历。为什么叫sixpoint，由第四级number取到的
                    for (int y = 0; y < sixthPointsArray.size(); y++) {
                        JSONObject sixPointObject = sixthPointsArray.getJSONObject(y);

                        //由第六级number拿到reducepoint的总分。reducepoint到底是个什么分
                        int deductSixthId = sixPointObject.getIntValue("sixthId");
                        float deductPointsSumFloat = parProblemCollectDetailMapper.getdeductSum(checkId, deductSixthId);
                        double deductPointsSum = Double.valueOf(String.valueOf(deductPointsSumFloat));

                        //6级的分
                        double truePoint = Double.parseDouble(sixPointObject.getString("point6"));
                        //calculatePoint66：6级的分减去reducepoint。为什么是减的总分
                        if (truePoint <= deductPointsSum) {
                            calculatePoint66 = 0;
                        } else {
                            calculatePoint66 = truePoint - deductPointsSum;
                        }
                        calculateTruePoint += truePoint;
                        calculatePoint6 += calculatePoint66;
                    }

                    //无问题项的分。怎么确定的无问题项？这里直接拿的第六级
                    double noProblemPoint = 0;
                    //uncheckedItems是干啥的？无问题项。根据4级id拿6级id。为什么不直接遍历？非要换成jsonarray
                    List<Object> noProblemSixthIdList = mapperUtils.getUncheckeditemsMapper().getNoProblemSixthId(checkId, part2InnerObject.getIntValue("fourthId"));
                    String noProblemSixthIdInfo = JSON.toJSONString(noProblemSixthIdList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                    JSONArray noProblemSixthArray = JSONArray.parseArray(noProblemSixthIdInfo);
                    for (int m = 0; m < noProblemSixthArray.size(); m++) {
                        int noProblemSixthId = noProblemSixthArray.getIntValue(m);
                        double noProblem = Double.parseDouble(parSystemMapper.getPoint4ByPoint4Id(noProblemSixthId));
                        noProblemPoint += noProblem;
                    }

                    //checkItemPoint
                    double checkItemPoint = (calculatePoint6 + noProblemPoint) / (calculateTruePoint + noProblemPoint) * 100;

                    //limitFourhPoint是什么？？？
                    //拿最大风险等级？根据4级id
                    List<ProblemCollectDetail> checkItemCalcuList = parProblemCollectDetailMapper.getAllFourthInfo(checkId, part2FourthId);
                    String maxFourthRiskPoint = pointsCalculate.getMaxRiskPoint(checkItemCalcuList);
                    //最大风险等级的数据数量
                    int maxFourthRiskPointNum = parProblemCollectDetailMapper.getRiskPointCount(checkId, part2FourthId, maxFourthRiskPoint);
                    //limitPoint是干啥的？打分？
                    double limitFourhPoint = Double.parseDouble((mapperUtils.getPointlimitMapper().getLimitPointByInfo(String.valueOf(maxFourthRiskPointNum), maxFourthRiskPoint)) == null ? "100" : mapperUtils.getPointlimitMapper().getLimitPointByInfo(String.valueOf(maxFourthRiskPointNum), maxFourthRiskPoint));

                    //处理checkItemPoint
                    if (checkItemPoint > limitFourhPoint) {
                        checkItemPoint = limitFourhPoint;
                    }
                    //两位小数，string
                    String s = df.format(checkItemPoint);
                    part2InnerMap.put("checkItemPoint", s);

                    //allpointsave是用来干嘛的？计算完分后，存一下
                    //number为-1时，存的总分。
                    int allPointSaveFourthPointCount = mapperUtils.getAllpointsaveMapper().getAllPointCountByCheckId(checkId, part2FourthId);
                    if (allPointSaveFourthPointCount != 0) {
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                        Date date1 = new Date(System.currentTimeMillis());
                        String lastEditTime = formatter.format(date1);
                        mapperUtils.getAllpointsaveMapper().updateAllPoint(checkId, s, part2FourthId, lastEditTime);
                    } else {
                        Allpointsave allpointsave = new Allpointsave();
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                        Date date1 = new Date(System.currentTimeMillis());
                        allpointsave.setNewCreateTime(formatter.format(date1));
                        allpointsave.setLastEditTime(formatter.format(date1));
                        allpointsave.setProjectId(projectId);
                        allpointsave.setCheckId(checkId);
                        allpointsave.setNumber(part2FourthId);
                        allpointsave.setPoint(s);
                        mapperUtils.getAllpointsaveMapper().insert(allpointsave);
                    }

                    //干啥的？四级的分？thirdPoint？secondPoint？
                    point += calculatePoint6 + noProblemPoint;
                    point11 += calculateTruePoint + noProblemPoint;
                }
                //分*权重，满分100分，权重归一
                double thirdPoint = Double.parseDouble(df.format(point / point11 * 100));
                double percent2 = Double.parseDouble(df.format(weight222 / allweight2));
                double secondPoint = Double.parseDouble(df.format(thirdPoint * percent2));
                //用于计算质量风险的
                allsecondPoint += secondPoint;
            }

            //更新了一些表
            //1级
            int firstPointCount = mapperUtils.getAllpointsaveMapper().getAllPointCountByCheckId(checkId, 1);
            if (firstPointCount != 0) {
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                Date date1 = new Date(System.currentTimeMillis());
                String lastEditTime = formatter.format(date1);
                mapperUtils.getAllpointsaveMapper().updateAllPoint(checkId, df.format(allsecondPoint), 1, lastEditTime);
            } else {
                Allpointsave allpointsave = new Allpointsave();
                allpointsave.setCheckId(checkId);
                allpointsave.setNumber(1);
                allpointsave.setPoint(df.format(allsecondPoint));
                allpointsave.setProjectId(projectId);
                mapperUtils.getAllpointsaveMapper().insert(allpointsave);
            }
            //算权重
            double trueWeight1 = Double.parseDouble(df.format(Tweight1 / allweight1));
            float allExtra = parProblemCollectDetailMapper.getAllExtraPoint(checkId);
            double allExtraPoints = Double.valueOf(String.valueOf(allExtra));
            overAllPoint += Double.parseDouble(df.format(trueWeight1 * allsecondPoint - allExtraPoints));
        }

        //设置工程的LatestRiskLevel，是什么？
        List<ProblemCollectDetail> overallList = parProblemCollectDetailMapper.getAllProblemCollectInfoByCheckId(checkId);
        ContractTaskProject contractTaskProjectRiskPoint = new ContractTaskProject();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date1 = new Date(System.currentTimeMillis());
        contractTaskProjectRiskPoint.setLastEditTime(formatter.format(date1));
        contractTaskProjectRiskPoint.setProjectId(projectId);
        contractTaskProjectRiskPoint.setLatestRiskLevel(pointsCalculate.getMaxRiskPoint(overallList));
        mapperUtils.getContractTaskProjectMapper().updateByPrimaryKeySelective(contractTaskProjectRiskPoint);

        //设置工程的LatestCheckTime
        ContractTaskProject newRiskList = mapperUtils.getContractTaskProjectMapper().selectByPrimaryKey(projectId);
        String finishTime = mapperUtils.getContractchecktaskMapper().getFinishTimeByCheckId(checkId);
        if (newRiskList.getCheckTime().equals("") || newRiskList.getCheckTime().compareTo(finishTime) <= 0) {
            try {
                ContractTaskProject contracttaskproject = new ContractTaskProject();
                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
                Date date2 = new Date(System.currentTimeMillis());
                contracttaskproject.setLastEditTime(formatter1.format(date2));
                contracttaskproject.setProjectId(projectId);
                contracttaskproject.setLatestCheckTime(finishTime);
                mapperUtils.getContractTaskProjectMapper().updateByPrimaryKeySelective(contracttaskproject);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        //2879是管理行为下的中建三局绿色公司(质量）。拿3级id
        List<Integer> ZJThirdIdList = parProblemCollectDetailMapper.getAllZJthirdId(checkId, 2879);
        List<Object> ZJList = new ArrayList<Object>();
        for (int t = 0; t < ZJThirdIdList.size(); t++) {
            Map<String, Object> ZJMap = new HashMap<String, Object>();
            String ZJtype = "";
            double ZJallOverPoint = 0;
            double ZJthirdPoint = 0;
            //怎么算的？逻辑
            //实测实量，0
            ZJMap.put("scAllPoint", scAllPoint);
            //管理行为计算在下面
            ZJMap.put("thirdPoint", "\\");
            //序号
            ZJMap.put("No", t + 1);
            //质量风险
            ZJMap.put("zlAllPoint", allsecondPoint - allExtraPoint);
            //评估对象
            ZJMap.put("pName3", parSystemMapper.getName4ByName4Id(ZJThirdIdList.get(t)));
            //评估类型
            switch (parSystemMapper.getName4ByName4Id(ZJThirdIdList.get(t))) {
                case "投资项目":
                    ZJtype = "投资管理";
                    break;
                case "监理":
                    ZJtype = "投资管理";
                    break;
                case "EPC(联营)":
                    ZJtype = "EPC";
                    break;
                case "EPC(自营)":
                    ZJtype = "EPC";
                    break;
                case "PC工厂":
                    ZJtype = "PC生产管理";
                    break;
                default:
                    ZJtype = "";
                    break;
            }
            //根据第三级id拿到对象数组，即第四级？
            List<ProblemCollectDetail> ZJfourthPoints = parProblemCollectDetailMapper.getZJFourthPoint(checkId, ZJThirdIdList.get(t));
            String ZJfourthCalculateInfo = JSON.toJSONString(ZJfourthPoints, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray ZJfourthPointsCalculateArray = JSONArray.parseArray(ZJfourthCalculateInfo);

            //减
            double point1 = 0;
            //没减
            double point2 = 0;
            for (int j = 0; j < ZJfourthPointsCalculateArray.size(); j++) {
                JSONObject ZJpart2InnerObject = ZJfourthPointsCalculateArray.getJSONObject(j);

                //groupby six为什么就是sixpoint
                int ZJpart2FourthId = ZJpart2InnerObject.getIntValue("fourthId");
                List<ProblemCollectDetail> ZJsixthPoints = parProblemCollectDetailMapper.getZJsixthPoints(checkId, ZJpart2FourthId);
                String ZJpointName6Info = JSON.toJSONString(ZJsixthPoints, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray ZJsixthPointsArray = JSONArray.parseArray(ZJpointName6Info);

                //和上面一样的逻辑，算分
                double ZJcalculatePoint6 = 0;
                double ZJcalculatePoint66 = 0;
                double ZJcalculateTruePoint = 0;
                for (int y = 0; y < ZJsixthPointsArray.size(); y++) {
                    JSONObject ZJsixPointObject = ZJsixthPointsArray.getJSONObject(y);
                    int ZJdeductSixthId = ZJsixPointObject.getIntValue("sixthId");
                    float ZJdeductPointsSumFloat = parProblemCollectDetailMapper.getdeductSum(checkId, ZJdeductSixthId);
                    double ZJdeductPointsSum = Double.valueOf(String.valueOf(ZJdeductPointsSumFloat));
                    double ZJtruePoint = Double.parseDouble(ZJsixPointObject.getString("point6"));
                    if (ZJtruePoint <= ZJdeductPointsSum) {
                        ZJcalculatePoint66 = 0;
                    } else {
                        ZJcalculatePoint66 = ZJtruePoint - ZJdeductPointsSum;
                    }
                    ZJcalculateTruePoint += ZJtruePoint;
                    ZJcalculatePoint6 += ZJcalculatePoint66;
                }

                //管理行为无问题项的分
                double ZJnoProblemPoint = 0;
                List<Object> ZJnoProblemSixthIdList = mapperUtils.getUncheckeditemsMapper().getNoProblemSixthId(checkId, ZJpart2FourthId);
                String ZJnoProblemSixthIdInfo = JSON.toJSONString(ZJnoProblemSixthIdList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray ZJnoProblemSixthArray = JSONArray.parseArray(ZJnoProblemSixthIdInfo);
                for (int m = 0; m < ZJnoProblemSixthArray.size(); m++) {
                    int ZJnoProblemSixthId = ZJnoProblemSixthArray.getIntValue(m);
                    double ZJnoProblem = Double.parseDouble(parSystemMapper.getPoint4ByPoint4Id(ZJnoProblemSixthId));
                    ZJnoProblemPoint += ZJnoProblem;
                }

                //ZJcheckItemPoint
                double ZJcheckItemPoint = (ZJcalculatePoint6 + ZJnoProblemPoint) / (ZJcalculateTruePoint + ZJnoProblemPoint) * 100;
                List<ProblemCollectDetail> ZJcheckItemCalcuList = parProblemCollectDetailMapper.getAllFourthInfo(checkId, ZJpart2FourthId);
                String ZJmaxFourthRiskPoint = pointsCalculate.getMaxRiskPoint(ZJcheckItemCalcuList);
                int ZJmaxFourthRiskPointNum = parProblemCollectDetailMapper.getRiskPointCount(checkId, ZJpart2FourthId, ZJmaxFourthRiskPoint);
                double ZJlimitFourthPoint = Double.parseDouble((mapperUtils.getPointlimitMapper().getLimitPointByInfo(String.valueOf(ZJmaxFourthRiskPointNum), ZJmaxFourthRiskPoint)) == null ? "100" : mapperUtils.getPointlimitMapper().getLimitPointByInfo(String.valueOf(ZJmaxFourthRiskPointNum), ZJmaxFourthRiskPoint));
                if (ZJcheckItemPoint > ZJlimitFourthPoint) {
                    ZJcheckItemPoint = ZJlimitFourthPoint;
                }
                String s = df.format(ZJcheckItemPoint);

                point1 += ZJcalculatePoint6 + ZJnoProblemPoint;
                point2 += ZJcalculateTruePoint + ZJnoProblemPoint;
                //ZJpoint += ZJcheckItemPoint * pointsCalculate.getPercentInstance(ZJpart2InnerObject.getString("weight3")) / ZJfourthPointsCalculateArray.size();
            }
            ZJthirdPoint = (point1 / point2) * 100;
            ZJMap.put("type", ZJtype);
            String ZJthird = df.format(ZJthirdPoint);
            ZJMap.put("thirdPoint", ZJthird);
            //综合评分
            if (scAllPoint == 0) {
                ZJallOverPoint = allsecondPoint * 0.6 + ZJthirdPoint * 0.4;
            } else {
                ZJallOverPoint = scAllPoint * 0.3 + allsecondPoint * 0.4 + ZJthirdPoint * 0.3;
            }
            String ZJallOver = df.format(ZJallOverPoint);
            ZJMap.put("allOverPoint", ZJallOver);

            ZJList.add(ZJMap);
        }
        dataMap.put("ZJList", ZJList);

        //实测实量没有？？？
        //以下部分没有权重？

        //第一部分 --------- 二、评估结果 -------（二）分项评估结果 ------- 2.质量隐患
        //problemNum呢？
        List<Object> zlProblemList = new ArrayList<Object>();
        List<ProblemCollectDetail> ZJZLList = parProblemCollectDetailMapper.getAllInfoByFirstId(checkId, 628);
        for (int i = 0; i < ZJZLList.size(); i++) {
            Map<String, Object> zjZLMap = new HashMap<String, Object>();
            ProblemCollectDetail problemCollectDetail = ZJZLList.get(i);
            //报告的序号没改动
            zjZLMap.put("no", i + 1);
            zjZLMap.put("pName3", problemCollectDetail.getpName3());
            zjZLMap.put("pName4", problemCollectDetail.getpName4());
            if (problemCollectDetail.getIsRemark().equals("N")) {
                zjZLMap.put("problemDescription", problemCollectDetail.getProblemDescription());
            } else if (problemCollectDetail.getIsRemark().equals("Y")) {
                zjZLMap.put("problemDescription", problemCollectDetail.getRemark());
            }
            String problemListRiskPoint = "";
            switch (problemCollectDetail.getRiskPoint()) {
                case "提醒":
                    problemListRiskPoint = "提醒";
                    break;
                case "1":
                    problemListRiskPoint = "四级隐患";
                    break;
                case "2":
                    problemListRiskPoint = "三级隐患";
                    break;
                case "3":
                    problemListRiskPoint = "二级隐患";
                    break;
                case "4":
                    problemListRiskPoint = "一级隐患";
                    break;
                default:
                    problemListRiskPoint = "";
                    break;
            }
            zjZLMap.put("riskPoint", problemListRiskPoint);
            zlProblemList.add(zjZLMap);
        }
        dataMap.put("zlProblemList", zlProblemList);
        //第一部分 --------- 二、评估结果 -------（二）分项评估结果 ------- 3.管理行为
        //主题怎么实现的？
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
        //应用主题样式
        int width = 718;
        int height = 468;
        ChartFactory.setChartTheme(standardChartTheme);

        List<Object> zjglList = new ArrayList<Object>();
        //736管理行为
        List<Integer> ZJGLList = parProblemCollectDetailMapper.getAllThirdIdByFirstId(checkId, 736);
        //List<ProblemCollectDetail> ZJGLList=parProblemCollectDetailMapper.getAllInfoByFirstId(checkId,736);
        for (int i = 0; i < ZJGLList.size(); i++) {
            //JFreeChart，画图的
            DefaultPieDataset datasetPie = new DefaultPieDataset();

            //zjglInnerList里装zjGLInnerMap
            List<Object> zjglInnerList = new ArrayList<Object>();
            Map<String, Object> zjGLMap = new HashMap<String, Object>();
            zjGLMap.put("pName3", parSystemMapper.getName4ByName4Id(ZJGLList.get(i)));
            List<ProblemCollectDetail> ZJGL3List = parProblemCollectDetailMapper.getZJSixthPoint(checkId, ZJGLList.get(i));
            int zjglProblem3AllNum = parProblemCollectDetailMapper.getZJGLProblemNumByThirdId(checkId, ZJGLList.get(i));
            for (int j = 0; j < ZJGL3List.size(); j++) {
                Map<String, Object> zjGLInnerMap = new HashMap<String, Object>();
                ProblemCollectDetail problemCollectDetail3 = ZJGL3List.get(j);
                zjGLInnerMap.put("num", j + 1);
                zjGLInnerMap.put("pName4", problemCollectDetail3.getpName6());
                int zjglProblem3Num = parProblemCollectDetailMapper.getZJGLProblemNumBySixthId(checkId, problemCollectDetail3.getSixthId());
                zjGLInnerMap.put("problemNum", zjglProblem3Num);
                //一个根据第六级算的，一个根据第三级算的
                zjGLInnerMap.put("percent", pointsCalculate.caculateToPercent(zjglProblem3Num, zjglProblem3AllNum));
                zjglInnerList.add(zjGLInnerMap);
                datasetPie.setValue(problemCollectDetail3.getpName4() + ":" + pointsCalculate.caculateToPercent(zjglProblem3Num, zjglProblem3AllNum), new Double(zjglProblem3Num));
            }

            try {
                //画图
                JFreeChart chart = ChartFactory.createPieChart("管理行为问题分布饼图", datasetPie, true, true, false);
                final PiePlot plot = (PiePlot) chart.getPlot();
                plot.setStartAngle(270);
                plot.setForegroundAlpha(0.90f);
                plot.setInteriorGap(0.02);
                chart.getLegend().setVisible(false);
                //File pieChart = new File("C:\\Users\\96500\\Desktop\\test\\" + fileName + "pieChart.png");
                File pieChart = new File(IMG_PATH + fileName + i + "pieChart.png");
                ChartUtils.saveChartAsPNG(pieChart, chart, width, height);
            } catch (Exception e) {
                e.printStackTrace();
            }

            zjGLMap.put("pic", MDoc.getImageStr(IMG_PATH + fileName + i + "pieChart.png"));
            zjGLMap.put("zjglInnerList", zjglInnerList);
            zjglList.add(zjGLMap);
        }
        dataMap.put("zjglList", zjglList);

        //第二部分 ------- 质量隐患具体检查情况
        String section2pName3 = "";
        List<Integer> name3List = parProblemCollectDetailMapper.getAllThirdIdByFirstId(checkId, 628);
        List<Object> section2List = new ArrayList<Object>();
        for (int b = 0; b < name3List.size(); b++) {
            Map<String, Object> section2Name3Map = new HashMap<String, Object>();
            List<Object> section2name3List = new ArrayList<Object>();
            section2pName3 = parSystemMapper.getName4ByName4Id(name3List.get(b));
            section2Name3Map.put("section2pName3", section2pName3);

            List<Object> section2Name4Info = parProblemCollectDetailMapper.getAllName4ByThirdId(checkId, name3List.get(b));
            String section2Name44 = JSON.toJSONString(section2Name4Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray section2Name4Array = JSONArray.parseArray(section2Name44);
            for (int w = 0; w < section2Name4Array.size(); w++) {
                Map<String, Object> section2Name4Map = new HashMap<String, Object>();
                List<Object> section2Name4List = new ArrayList<Object>();
                int section2Name4Id = section2Name4Array.getIntValue(w);

                String section2Name4 = parSystemMapper.getName4ByName4Id(section2Name4Id);
                //为社么叫name6
                List<ProblemCollectDetail> section2Name6Info = parProblemCollectDetailMapper.getAllName6InfoByName4(checkId, section2Name4Id, "质量检查");
                String section2Name66 = JSON.toJSONString(section2Name6Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray section2Name6Array = JSONArray.parseArray(section2Name66);
                for (int c = 0; c < section2Name6Array.size(); c++) {
                    Map<String, Object> section2Name6Map = new HashMap<String, Object>();
                    List<Object> picList = new ArrayList<Object>();
                    JSONObject section2Name6Object = section2Name6Array.getJSONObject(c);
                    section2Name6Map.put("section2pName6No", c + 1);
                    section2Name6Map.put("section2pName6", section2Name6Object.getString("pName6"));
                    section2Name6Map.put("section2pName5", section2Name6Object.getString("pName5"));
                    section2Name6Map.put("section2RiskLevel", section2Name6Object.getString("riskPoint"));
                    section2Name6Map.put("section2CheckPart", section2Name6Object.getString("checkPart"));
                    section2Name6Map.put("section2ProblemNum", section2Name6Object.getString("problemQua"));
                    if (section2Name6Object.getString("isRemark").equals("N")) {
                        section2Name6Map.put("section2problemDescription", section2Name6Object.getString("problemDescription"));
                    } else if (section2Name6Object.getString("isRemark").equals("Y")) {
                        section2Name6Map.put("section2problemDescription", section2Name6Object.getString("remark"));
                    }
                    section2Name6Map.put("section2normsProvison", section2Name6Object.getString("normsProvison"));
                    section2Name6Map.put("section2suggestion", section2Name6Object.getString("suggestion"));
                    String section2picPath = section2Name6Object.getString("picPath");
                    String picName = section2Name6Object.getString("picName");
                    JSONArray ja = JSONArray.parseArray(picName);
                    if (ja.size() == 0) {
                        //String s = MDoc.getImageStr("C:\\Users\\96500\\Desktop\\20190329183836002.png");
                        String s = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                        picList.add(s);
                    } else {
                        for (int o = 0; o < ja.size(); o++) {
                            String s = "";
                            if ("prod".equals(profiles.getActives())) {
                                s = MDoc.getImageStr(section2picPath + ja.getString(o));
                            }
                            //String s = MDoc.getImageStr("C:\\Users\\96500\\Desktop\\20190329183836002.png");
                            picList.add(s);
                        }
                    }
                    section2Name6Map.put("picList", picList);
                    section2Name4List.add(section2Name6Map);

                }
                section2Name4Map.put("section2pName4", section2Name4);
                section2Name4Map.put("section2Name4List", section2Name4List);
                section2name3List.add(section2Name4Map);
            }
            section2Name3Map.put("section2Name3List", section2name3List);
            section2List.add(section2Name3Map);
        }
        dataMap.put("section2List", section2List);

        //第三部分 ------- 管理行为具体检查情况
        //同上
        String section3pName3 = "";
        List<Integer> glName3List = parProblemCollectDetailMapper.getAllThirdIdByFirstId(checkId, 736);
        List<Object> section3List = new ArrayList<Object>();
        for (int b = 0; b < glName3List.size(); b++) {
            Map<String, Object> section3Name3Map = new HashMap<String, Object>();
            List<Object> section3name3List = new ArrayList<Object>();
            section3pName3 = parSystemMapper.getName4ByName4Id(glName3List.get(b));
            section3Name3Map.put("section3pName3", section3pName3);
            List<Object> section3Name4Info = parProblemCollectDetailMapper.getAllName4ByThirdId(checkId, glName3List.get(b));
            String section3Name44 = JSON.toJSONString(section3Name4Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray section3Name4Array = JSONArray.parseArray(section3Name44);
            for (int w = 0; w < section3Name4Array.size(); w++) {
                Map<String, Object> section3Name4Map = new HashMap<String, Object>();
                List<Object> section3Name4List = new ArrayList<Object>();
                int section3Name4Id = section3Name4Array.getIntValue(w);
                String section3Name4 = parSystemMapper.getName4ByName4Id(section3Name4Id);
                List<ProblemCollectDetail> section3Name6Info = parProblemCollectDetailMapper.getAllName6InfoByName4(checkId, section3Name4Id, "管理行为");
                String section3Name66 = JSON.toJSONString(section3Name6Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
                JSONArray section3Name6Array = JSONArray.parseArray(section3Name66);
                for (int c = 0; c < section3Name6Array.size(); c++) {
                    Map<String, Object> section3Name6Map = new HashMap<String, Object>();
                    List<Object> picList = new ArrayList<Object>();
                    JSONObject section3Name6Object = section3Name6Array.getJSONObject(c);
                    section3Name6Map.put("section3pName6No", c + 1);
                    section3Name6Map.put("section3pName6", section3Name6Object.getString("pName6"));
                    section3Name6Map.put("section3pName5", section3Name6Object.getString("pName5"));
                    section3Name6Map.put("section3RiskLevel", section3Name6Object.getString("riskPoint"));
                    section3Name6Map.put("section3CheckPart", section3Name6Object.getString("checkPart"));
                    section3Name6Map.put("section3ProblemNum", section3Name6Object.getString("problemQua"));
                    if (section3Name6Object.getString("isRemark").equals("N")) {
                        section3Name6Map.put("section3problemDescription", section3Name6Object.getString("problemDescription"));
                    } else if (section3Name6Object.getString("isRemark").equals("Y")) {
                        section3Name6Map.put("section3problemDescription", section3Name6Object.getString("remark"));
                    }
                    section3Name6Map.put("section3normsProvison", section3Name6Object.getString("normsProvison"));
                    section3Name6Map.put("section3suggestion", section3Name6Object.getString("suggestion"));
                    String section3picPath = section3Name6Object.getString("picPath");
                    String picName = section3Name6Object.getString("picName");
                    JSONArray ja = JSONArray.parseArray(picName);
                    if (ja.size() == 0) {
                        //String s = MDoc.getImageStr("C:\\Users\\96500\\Desktop\\20190329183836002.png");
                        String s = MDoc.getImageStr("D:\\whutServer\\generatedCharts\\noPic.png");
                        picList.add(s);
                    } else {
                        for (int o = 0; o < ja.size(); o++) {
                            String s = "";
                            if ("prod".equals(profiles.getActives())) {
                                s = MDoc.getImageStr(section3picPath + ja.getString(o));
                            }
                            //String s = MDoc.getImageStr("C:\\Users\\96500\\Desktop\\20190329183836002.png");
                            picList.add(s);
                        }
                    }
                    section3Name6Map.put("picList", picList);
                    section3Name4List.add(section3Name6Map);

                }
                section3Name4Map.put("section3pName4", section3Name4);
                section3Name4Map.put("section3Name4List", section3Name4List);
                section3name3List.add(section3Name4Map);
            }
            section3Name3Map.put("section3Name3List", section3name3List);
            section3List.add(section3Name3Map);
        }
        dataMap.put("section3List", section3List);

        //第四部分 ------- 改进建议
        List<Integer> pName1Info = parProblemCollectDetailMapper.getAllName1Id(checkId);
        String name1 = JSON.toJSONString(pName1Info, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        JSONArray name1Array = JSONArray.parseArray(name1);
        List<Object> part4List = new ArrayList<Object>();
        for (int i = 0; i < name1Array.size(); i++) {
            List<Object> part4InnerList = new ArrayList<Object>();
            Map<String, Object> part4Map = new HashMap<String, Object>();
            String pName1 = parSystemMapper.getName4ByName4Id(name1Array.getIntValue(i));
            //改进建议
            List<Summary> summaryList = mapperUtils.getSummaryMapper().getSummaryInfo(checkId, name1Array.getIntValue(i));
            String summaryInfo = JSON.toJSONString(summaryList, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
            JSONArray summaryInfoArray = JSONArray.parseArray(summaryInfo);
            for (int j = 0; j < summaryInfoArray.size(); j++) {
                Map<String, Object> part4InnerMap = new HashMap<String, Object>();
                JSONObject part4InnerObject = summaryInfoArray.getJSONObject(j);
                String part4pNname4 = parSystemMapper.getName4ByName4Id(part4InnerObject.getIntValue("fourthId"));

                //拿到上一级的名字，即name3
                ParPrefixSystem prefixsystem = parSystemMapper.getFather(part4InnerObject.getIntValue("fourthId"));
                String part4pName3 = parSystemMapper.getName4ByName4Id(prefixsystem.getPreNum());

                part4InnerMap.put("pName3", part4pName3);
                part4InnerMap.put("no", j + 1);
                part4InnerMap.put("pName4", part4pNname4);
                part4InnerMap.put("summary", part4InnerObject.getString("summary"));
                part4InnerList.add(part4InnerMap);
            }
            part4Map.put("name1", pName1);
            part4Map.put("part4InnerList", part4InnerList);
            part4List.add(part4Map);
        }
        dataMap.put("part4List", part4List);

        MDoc mdoc = new MDoc();
        String path = FileUtil.getServerFolderPath();
        File f1 = new File(path, "doc");
        if (!f1.exists()) {
            f1.mkdirs();
        }
        mdoc.createDoc(dataMap, path + "\\doc\\" + dataMap.get("projectName").toString().replace('#', '号') + "质量专项报告" + fileName + ".doc", "ftl\\ZJ2.0.ftl");
        //MDoc.docToDocx("D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\"+ dataMap.get("projectName").toString().replace('#','号')+"质量专项报告"+fileName + ".doc", "D:\\whutServer\\apache-tomcat-8.5.41\\webapps\\ROOT\\doc\\" + dataMap.get("projectName").toString().replace('#','号')+"质量专项报告"+fileName +".doc");
        String result = urlProperties.getAddress() + "/doc/" + dataMap.get("projectName").toString().replace('#', '号') + "质量专项报告" + fileName + ".doc";
        return result;
    }
}
